package com.ws.soa.consumer.config;

import com.ws.soa.consumer.service.RedisCacheClientService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

/**
 * WxUserController
 *
 * @author luobosi@2dfire.com
 * @since 2019-03-02
 */
@Configuration
public class RedisConfig {

    /**
     * jedisPoolConfig config
     */
    @Bean(name = "jedisPoolConfig")
    public JedisPoolConfig jedisPoolConfig(@Qualifier("redisProperties") RedisProperties redisProperties) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
        jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());
        jedisPoolConfig.setMaxTotal(redisProperties.getMaxTotal());
        jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
        jedisPoolConfig.setTestOnBorrow(false);
        jedisPoolConfig.setTestOnReturn(false);
        return jedisPoolConfig;
    }

    /**
     * JedisPool
     */
    @Bean(name = "redisCacheClientService", destroyMethod = "destroy")
    public RedisCacheClientService jedisConnectionFactory(@Qualifier("jedisPoolConfig") JedisPoolConfig jedisPoolConfig,
                                                          @Qualifier("redisProperties") RedisProperties redisProperties) {
        RedisCacheClientService redisCacheClientService = new RedisCacheClientService();
        JedisPool pool = new JedisPool(jedisPoolConfig, redisProperties.getHost(), redisProperties.getPort(), Protocol.DEFAULT_TIMEOUT, redisProperties.getPassword(), redisProperties.getDatabase());
        redisCacheClientService.setPool(pool);
        return redisCacheClientService;
    }

    /**
     * business JedisPool
     */
    @Bean(name = "businessRedisClientService", destroyMethod = "destroy")
    public RedisCacheClientService businessJedisConnectionFactory(@Qualifier("jedisPoolConfig") JedisPoolConfig jedisPoolConfig,
                                                                  @Qualifier("businessRedisConfig") BusinessRedisConfig businessRedisConfig) {
        RedisCacheClientService redisCacheClientService = new RedisCacheClientService();
        JedisPool pool = new JedisPool(jedisPoolConfig, businessRedisConfig.getHost(), businessRedisConfig.getPort(), Protocol.DEFAULT_TIMEOUT, businessRedisConfig.getPassword(), businessRedisConfig.getDatabase());
        redisCacheClientService.setPool(pool);
        return redisCacheClientService;
    }


}

